/**
 * Created by WheelChen on 2017/7/6.
 */

layui.config({
    base: '../../static/js/layuiModulesEX/'
});
layui.use(['paging', 'form','laytpl'], function () {
    var $ = layui.jquery,
        paging = layui.paging(),
        layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
        layer = layui.layer, //获取当前窗口的layer对象
        laytpl = layui.laytpl,
        form = layui.form();

    // console.log(teacher.id);
    paging.init({
        openWait: true,
        url: '/internInfo/findBatchByCondition2',
        // url:'/student/findDetailBatchByInternAdvisor',
        elem: '#content', //内容容器
        params: { //发送到服务端的参数
            internadvisor: teacher.id
        },
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        tempElem: '#tpl', //模块容器
        pageConfig: { //分页参数配置
            elem: 'paged', //分页容器
            pageSize: 10 //分页大小
        },
        success: function () { //渲染成功的回调
            //alert('渲染成功');
        },
        fail: function (msg) { //获取数据失败的回调
            //alert('获取数据失败')
        },
        complate: function () { //完成的回调
            //绑定所有编辑按钮事件
            $('#content').children('tr').each(function () {
                //获取每行记录的id值
                var $that = $(this).children('td:last-child');
                var $org = $(this).children('td:nth-child(5)');
                var orgId = $org.children('a[data-opt=org]').data('id');
                var studentUserId = $that.children('a[data-opt=more]').data('id');
                var studentInfoId = $that.children('a[data-opt=appraise]').data('id');

                // 详情按钮
                $that.children('a[data-opt=more]').on('click', function () {
                    axios.get('/student/findDetail/' + studentUserId)
                        .then(function (Response) {
                            console.log(Response.data.data);
                            vm.user = Response.data.data.user;
                            vm.studentInfo = Response.data.data.studentInfo;
                            vm.university = Response.data.data.university;
                            vm.school = Response.data.data.school;
                            vm.department = Response.data.data.department;
                            vm.major = Response.data.data.major;
                            vm.teacherInfoInternName = Response.data.data.teacherInfoInternName;
                            vm.teacherInfoPaperName = Response.data.data.teacherInfoPaperName;
                            vm.clazz = Response.data.data.clazz;
                            $('#advisorappraise').text('');
                            $('#advisorappraise').html(vm.studentInfo.advisorappraise);
                            vm.show = false;
                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                });

                // 实习评价按钮
                $that.children('a[data-opt=appraise]').on('click',function () {
                     location.href='/teacher/studentInternAppraise?studentInfoUserid='+studentUserId;
                });

                // 实习情况按钮
                $that.children('a[data-opt=internInfo]').on('click',function () {
                    location.href='/teacher/internInfoEverytStudent?studentUserId='+studentUserId;
                })

                if($that.children('a[data-opt=accept]')) {
                    var studentInfoId_accept = $that.children('a[data-opt=accept]').data('id');
                }
                if($that.children('a[data-opt=review]')) {
                    var studentInfoId_review = $that.children('a[data-opt=review]').data('id');
                }
                if($that.children('a[data-opt=internChange]')) {
                    var studentInfoId_internChange = $that.children('a[data-opt=internChange]').data('id');
                }
                // console.log("studentInfoId_accept:"+studentInfoId_accept);
                // console.log("studentInfoId_review:"+studentInfoId_review)

                /**
                 * 接受学生按钮
                 */
                $that.children('a[data-opt=accept]').on('click', function () {
                    layer.confirm('是否成为该学生的指导老师',function (index) {
                        axios.post('/student/modifyOneById',{
                            id:studentInfoId_accept,
                            advisestate:2
                        })
                            .then(function (Response) {
                                console.log(Response.data);
                                //重新加载页面
                                paging.get();
                                layer.msg('操作成功',{icon:1, time:1500},function () {
                                    layer.close(index);
                                })
                            })
                            .catch(function (response) {
                                console.log(response);
                                layer.msg('操作失败',{icon:5, time:1500},function () {
                                    layer.close(index);
                                })
                            });
                    });

                });

                /**
                 * 实习审核按钮(获得对应studentInfo表id及状态为待审核的一条记录（确保只有一条记录）)
                 */
                $that.children('a[data-opt=review]').on('click',function () {
                    axios.post('/internInfo/findByCondition',{
                        sno:studentInfoId_review,
                        state:2
                    })
                        .then(function (response) {
                            console.log(response.data);
                            if(response.data.data.length!==0) {
                                $('#workEnvironment').attr('src',"");
                                vm.internInfoDetail = response.data.data[0];
                                vm.internInfo = vm.internInfoDetail.internInfo;
                                vm.studentInfo = vm.internInfoDetail.studentInfoDetail.studentInfo;
                                console.log(vm.internInfoDetail);
                                console.log(vm.studentInfo);
                                var auditIndex = layer.open({
                                    type: 1,
                                    title: "实习信息",
                                    content: laytpl($('#detailTpl').html()).render(vm.internInfoDetail),
                                    shade: [0.5, '#FFF'], //模态框
                                    area: ['700px', '600px'],
                                    zIndex: 19950924,
                                    btn: ['通过', '不通过', '取消'],
                                    yes: function (index,layero) {
                                        // console.log("happy");
                                        top.layer.confirm("确认通过吗？",function (index) {
                                            vm.internInfo.state = 3;
                                            vm.internInfo.auditsug = $("#auditsug").val();
                                            // console.log(vm.internInfo);
                                            axios.post("/internInfo/modify", vm.internInfo)
                                                .then(function (response) {
                                                    console.log(response);
                                                    if (response.data.success) {
                                                        layer.msg('审核通过操作成功', {icon: 1, time: 1000});
                                                    }
                                                    else {
                                                        layer.msg('操作失败', {icon: 2, time: 1000});
                                                    }
                                                    top.layer.close(index);
                                                    layer.close(auditIndex);
                                                    paging.init();
                                                })
                                                .catch(function (response) {
                                                    console.log(response);
                                                })
                                        });
                                    },
                                    btn2:function (index,layero) {
                                        console.log(vm.internInfo);
                                        top.layer.prompt({
                                            formType: 2,
                                            title: "意见",
                                            value: "",
                                            area: ['300px', '150px'],
                                            // btn: ['是的', '放过'],
                                        },function(value,index,elem){
                                            console.log(value);
                                            vm.internInfo.state = 4;
                                            vm.internInfo.auditsug = value;
                                            vm.studentInfo.state = 1;   // 拒绝申请，状态回复为1-未实习
                                            console.log(vm.studentInfo);
                                            console.log(vm.internInfo);
                                            axios.all([axios.post('/internInfo/modify',vm.internInfo),axios.post('/student/modifyOneById',vm.studentInfo)])
                                                .then(function (response) {
                                                    var res_internInfo = response[0];
                                                    var res_studentInfo = response[1];
                                                    if(res_internInfo.data.success&&res_studentInfo.data.success){
                                                        top.layer.msg('审核不通过操作成功', {icon: 1, time: 1500});
                                                        top.layer.close(index);
                                                        layer.close(auditIndex);
                                                        paging.init();
                                                    }
                                                })
                                                .catch(function (msg) {
                                                    console.log(msg);
                                                    console.log("操作失败");
                                                });

                                        });
                                        return false;
                                    },
                                    end: function (index,layero) {
                                        layer.close(index);
                                    }
                                });
                                //改变图片显示
                                if (response.data.data.image !== null) {
                                    $('#workEnvironment').attr('src', vm.internInfoDetail.image.path);
                                }
                            }
                            else{
                                layer.msg("暂无实习情况");
                            }
                        })
                        .catch(function (response) {
                            console.log(response);
                        })
                });

                /**
                 * 实习日志按钮（新建jsp页面并传递参数）
                 */
                $that.children('a[data-opt=internLog]').on('click',function () {
                    // console.log(userId);
                    location.href='/teacher/internDiaryEveryStudent?studentUserId=' + studentUserId;
                });

                /**
                 * 企业链接
                 */
                $org.children('a[data-opt=org]').on('click',function () {
                    // console.log(orgId);
                    location.href="/teacher/orgInfo?origin='/teacher/internLists'&orgId="+orgId+"&&operateType=2";
                });

                /**
                 * 实习变更申请审核按钮
                 */
                $that.children('a[data-opt=internChange]').on('click',function () {
                    location.href="/teacher/internChangeReview?studentInfoId="+studentInfoId_internChange+'&studentUserId='+studentUserId;
                })
            });

        }
    });

    var vm = new Vue({
        el: '#vue'
        , data: {
            show: true
            , user: {
                id: teacherUserId
            }
            , studentInfo: {}
            , university: {}
            , school: {}
            , department: {}
            , major: {}
            , clazz: {}
            , teacherInfoInternName: ''
            , teacherInfoPaperName: ''
            , internInfoDetail:{}
            , internInfo:{}
        }
        , methods: {
            reload: function () {
                vm.show = true;
            }
            , search: function () {

            }
            , add: function () {
                vm.show = true;
            }
        }
    });


});